package com.burgeon.backend.SHOPBACKEND.rest.marketing;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.MarketingItem;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QueryMarketingItem extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public QueryMarketingItem(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<MarketingItem> getData(String id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(id), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<MarketingItem> marketingArrayList = new ArrayList<MarketingItem>();
        for (int i=0;i<jsonArray.length();i++){
            JSONArray data = jsonArray.getJSONArray(i);
            MarketingItem marketingItem = new MarketingItem();
            marketingItem.setId(data.getString(0));
            marketingItem.setNum(data.getString(1));
            marketingItem.setDescriptio(data.getString(2));
            marketingItem.setName(data.getString(3));
            marketingArrayList.add(marketingItem);
        }

        return marketingArrayList;
    }

    private MultiValueMap Data(String id) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("VIP_QTY");//
        columns.add("DESCRIPTION");//
        columns.add("'YC_LABELDEFINE_ID;NAME'");


        params.put("columns", columns);
        params.put("table", "YC_LABELDEFINEITEM");//表名

        params.put("count","true");



        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        expr1.put("column", "'YC_LABELDEFINE_ID'");
        expr1.put("condition", id);

        expr2.put("column", "ISENABLE");
        expr2.put("condition", "Y");

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);
        params.put("params", paramss);
        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QueryMarketingItem queryMail = new QueryMarketingItem("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(queryMail.getData("12"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
